package com.itheima;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import com.itheima.domain.VO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
//component衍生注解在web层使用
@Controller
public class UserController {
    @RequestMapping("/quick22")
    @ResponseBody
    public void save22(String username, MultipartFile uploadFile) throws JsonProcessingException {
        System.out.println(username);
        //获得上传文件名称
        String originalFilename = uploadFile.getOriginalFilename();
        try {
            uploadFile.transferTo(new File("D:\\upload\\"+originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/quick21")
    @ResponseBody
    public void save21(@CookieValue(value="JSESSIONID") String jsessionId) throws JsonProcessingException {
        System.out.println(jsessionId);
    }
    @RequestMapping("/quick20")
    @ResponseBody
    public void save20(@RequestHeader(value="User-Agent") String user_agent) throws JsonProcessingException {
        System.out.println(user_agent);
    }
    @RequestMapping("/quick19")
    @ResponseBody
    public void save19(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws JsonProcessingException {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }
    //自定义转换器
    /*
    1.定义转换器类实现Converter接口
    2.在配置文件中声明转换器
    3.在<annotation-driven>中引用转换器
     */
    @RequestMapping("/quick18")
    @ResponseBody//告知SpringMVC框架,不进行视图跳转,直接进行数据响应
    public void save18(Date date) throws JsonProcessingException {
        System.out.println(date);
    }
    @RequestMapping("/quick17/{name}")
    @ResponseBody//告知SpringMVC框架,不进行视图跳转,直接进行数据响应
    public void save17(@PathVariable(value="name") String username) throws JsonProcessingException {
        System.out.println(username);
    }
    //当请求参数名称与Controller的业务方法参数名称不一致时,就需要通过@RequestParam注解显示地绑定
    @RequestMapping("/quick16")
    @ResponseBody//告知SpringMVC框架,不进行视图跳转,直接进行数据响应
    public void save16(@RequestParam("name") String username) throws JsonProcessingException {
        System.out.println(username);
    }
    @RequestMapping("/quick14")
    @ResponseBody
    public void save14(VO vo) throws JsonProcessingException {
        System.out.println(vo);
    }

    @RequestMapping("/quick13")
   @ResponseBody
    public void save13(String strs) throws JsonProcessingException {
        System.out.println(Arrays.asList(strs));
    }

    @RequestMapping("/quick12")
    @ResponseBody
    public void save12(User user) throws JsonProcessingException {
        System.out.println(user);
    }

    @RequestMapping("/quick11")
    @ResponseBody
    public void save11(String username,int age) throws JsonProcessingException {
        System.out.println(username);
        System.out.println(age);
    }

    @RequestMapping("/quick10")
    @ResponseBody//告知SpringMVC框架,不进行视图跳转,直接进行数据响应
    //期望SpringMVC自动将User转换成json格式的字符串,配置处理器适配器
    //springmvc的各个组件中，处理器映射器，处理器适配器，视图解析器称springmvc的三大组件
    public User save10() throws JsonProcessingException {
        User user=new User();
        user.setUsername("wangwu");
        user.setAge(30);
        return user;
    }
    @RequestMapping("/quick9")
    @ResponseBody//告知SpringMVC框架,不进行视图跳转,直接进行数据响应
    public String save9() throws JsonProcessingException {
        User user=new User();
        user.setUsername("lisi");
        user.setAge(30);
        //使用json的转换工具将对象转换成json格式字符串再返回
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }

    @RequestMapping("/quick8")
    @ResponseBody//告知SpringMVC框架,不进行视图跳转,直接进行数据响应
    public String save8() {
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }
    @RequestMapping("/quick7")
    @ResponseBody//告知SpringMVC框架,不进行视图跳转,直接进行数据响应
    public String save7(){
        return "hello itheima";
    }
    //这种方式不常用
    @RequestMapping("/quick5")
    public String save5(HttpServletRequest request){
        request.setAttribute("username","酷丁鱼");
        return "success";
    }
    @RequestMapping("/quick4")
    public String save4(Model model){
        model.addAttribute("username","博学谷");
        return "success";
    }
    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView){
        modelAndView.addObject("username","itheima");
        modelAndView.setViewName("success");//success.jsp
        return modelAndView;
    }
    @RequestMapping("/quick2")
    public ModelAndView save2(){
        /*
        Model作用是封装数据
        View作用是展示数据
         */
        ModelAndView modelAndView=new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","itcast");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }
    //@RequestMapping(value="/quick",method=RequestMethod.POST,params={"accountName"})
    @RequestMapping("/quick")
    public String save(){
        System.out.println("controller save running.....");
        return "success";
    }


}
